package com.designschool.base.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.designschool.activity.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;

/***
 * 
 * @项目名:longtu  
 * 
 * @类名:ScreenShot.java  
 * 
 * @创建人:shibaotong 
 *
 * @类描述:截屏
 * 
 * @date:2016年1月21日
 * 
 * @Version:1.0 
 *
 *****************************************
 */
public class ScreenShotUtils {

	public static void shoot(Activity a, File filePath) {
//	    Log.e("filePath==>", a.getApplicationContext().getFilesDir().getAbsolutePath()+"--"+filePath);
		if (filePath == null) {
			return;
		}
		if (!filePath.getParentFile().exists()) {
			filePath.getParentFile().mkdirs();
		}
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(filePath);
			if (null != fos) {
			    takeScreenShotCode(a).compress(Bitmap.CompressFormat.PNG, 100, fos);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.flush();
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private static Bitmap takeScreenShot(Activity activity) {
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap bitmap = view.getDrawingCache();
		Rect frame = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;
		int width = activity.getWindowManager().getDefaultDisplay().getWidth();
		int height = activity.getWindowManager().getDefaultDisplay()
				.getHeight(); 
		Bitmap b = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width,
				height - statusBarHeight);
		view.destroyDrawingCache();
		return b;
	}
	
	private static Bitmap takeScreenShotCode(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay()
                .getHeight(); 
        Bitmap b = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width,
                height - statusBarHeight);
        view.destroyDrawingCache();
        Canvas canvas=new Canvas(b);
        Paint paint=new Paint();
        paint.setColor(Color.BLACK);
        
        canvas.drawBitmap(b, new Matrix(), paint);
        Bitmap ic_launch=BitmapFactory.decodeResource(activity.getResources(), R.drawable.canvas);
        
        canvas.drawBitmap(ic_launch, (width-ic_launch.getWidth())/2, height-ic_launch.getHeight()*2, paint);
        return b;
    }


}
